﻿using Gadgeteer;
using GHI.Glide;
using GHI.Glide.Display;
using GHI.Glide.UI;
using Microsoft.SPOT;
using Microsoft.SPOT.Touch;
using PJE.GlideEx.UI;

namespace PagingTextBlockTest
{
	public partial class Program
	{
		private Window _window;
		private PagingTexBlock _pagingTexBlock;
		private Button _buttonStartTimer;
		private Button _buttonPageUp;
		private Button _buttonPageDown;
		private int _index = 1;
		private bool _countup = true;

		void ProgramStarted()
		{
			Debug.Print("Program Started");

			GlideTouch.Initialize();

			var sy = new[]
			{
				(short)136, (short)26, (short)246, (short)246, (short)26
			};

			var sx = new[]
			{
				(short)240,(short)48,(short)48,(short)432,(short)432
			};

			var cy = new[]
			{
				(short)482,(short)140,(short)815,(short)754,(short)176
			};

			var cx = new[]
			{
				(short)471,(short)99,(short)139,(short)830,(short)856
			};

			Touch.ActiveTouchPanel.SetCalibration(5, sx, sy, cx, cy);

			_window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.window));

			_buttonPageDown = (Button)_window.GetChildByName("buttonPageDown");
			_buttonPageUp = (Button)_window.GetChildByName("buttonPageUp");
			_buttonStartTimer = (Button)_window.GetChildByName("buttonStartTimer");

			_pagingTexBlock = new PagingTexBlock("ptb", 255, 0, 0, 272, 272);

			_window.AddChild(_pagingTexBlock);

			Glide.MainWindow = _window;

			var str = "";

			for (var i = 0; i < 1500; i++)
			{
				str += i + ": Glide is awesome!\r\n";
			}

			_pagingTexBlock.Text = str;

			_pagingTexBlock.SetPage(_index);

			_pagingTexBlock.Invalidate();

			_buttonPageDown.TapEvent += _buttonPageDown_TapEvent;

			_buttonPageUp.TapEvent += _buttonPageUp_TapEvent;

			_buttonStartTimer.TapEvent += _buttonStartTimer_TapEvent;

			_timer = new Timer(500);

			_timer.Tick += timer_Tick;
		}

		private Timer _timer;

		void _buttonStartTimer_TapEvent(object sender)
		{
			if (_timer.IsRunning)
				_timer.Stop();
			else
				_timer.Start();
			_buttonPageDown.Enabled = !_timer.IsRunning;
			_buttonPageUp.Enabled = !_timer.IsRunning;
			_buttonPageDown.Invalidate();
			_buttonPageUp.Invalidate();
			//throw new System.NotImplementedException();
		}

		void _buttonPageUp_TapEvent(object sender)
		{
			_index++;
			if (_index > _pagingTexBlock.TotalPages)
				_index = _pagingTexBlock.TotalPages;
			_pagingTexBlock.SetPage(_index);
			_pagingTexBlock.Invalidate();
			//throw new System.NotImplementedException();
		}

		void _buttonPageDown_TapEvent(object sender)
		{
			//throw new System.NotImplementedException();
			_index--;
			if (_index < 1)
				_index = 1;
			_pagingTexBlock.SetPage(_index);
			_pagingTexBlock.Invalidate();
		}

		void timer_Tick(Timer timer)
		{
			if (_countup)
			{
				_index++;
				_pagingTexBlock.SetPage(_index);
				_pagingTexBlock.Invalidate();
				if (_index == _pagingTexBlock.TotalPages)
					_countup = false;
			}
			else if (!_countup)
			{
				_index--;
				_pagingTexBlock.SetPage(_index);
				_pagingTexBlock.Invalidate();
				if (_index == 1)
					_countup = true;
			}
			//throw new System.NotImplementedException();
		}
	}
}
